package utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import model.Combo;
import model.Hero;
import model.HeroCombo;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;

/**
 * http://stackoverflow.com/questions/14018257/nested-xml-parsing-using-dom-
 * parsing
 * 
 * @author abc
 * 
 */
public class HeroXMLPullParser {

	XmlPullParserFactory pullParserFactory;
	XmlPullParser parser;

	public HeroXMLPullParser(Context appContext) {
		try {
			pullParserFactory = XmlPullParserFactory.newInstance();
			parser = pullParserFactory.newPullParser();

			InputStream is = appContext.getAssets().open("combos.xml");
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			parser.setInput(is, null);
			parser.nextTag();

		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public List<HeroCombo> parseXML() throws XmlPullParserException, IOException {

		List<HeroCombo> heroComboList = new ArrayList<HeroCombo>();

		parser.require(XmlPullParser.START_TAG, null, "combos");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}

			String name = parser.getName();
			if (name.equals("hero-combo")) {
				heroComboList.add(readHeroCombo(parser));
			} else {
				skip(parser);
			}
		}

		return heroComboList;

	}

	private HeroCombo readHeroCombo(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "hero-combo");
		System.out.println("@@@@ READING HEROCOMBO");
		Hero hero = null;
		List<Combo> combo = new ArrayList<Combo>();

		// int eventType = parser.getEventType();
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}

			String tagname = parser.getName();
			System.out.println("TAG NAME: " + tagname);

			if (tagname.equals("hero")) {
				System.out.println("PARSE HERO HERE");
				hero = readHero(parser);
			} else if (tagname.equals("combo")) {
				System.out.println("PARSE COMBO HERE");
				// combo.add(readCombo(parser));
			} else {
				System.out.println("SKIP");
				skip(parser);
			}

		}

		return new HeroCombo(hero, combo);
	}

	private Hero readHero(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "hero");
		System.out.println("@@@@ PARSING HERO");
		String heroName = null;
		String heroRole = null;

		// int eventType = parser.getEventType();
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();

			if (name.equals("heroName")) {
				heroName = parser.getText();
				System.out.println("heroName: " + heroName);
			} else if (name.equals("heroRole")) {
				heroRole = parser.getText();
				System.out.println("heroRole: " + heroRole);
			}
			// eventType = parser.nextTag();
		}

		return new Hero(heroName, heroRole);
	}

	private Combo readCombo(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "combo");
		System.out.println("@@@@ READING COMBO");
		String heroName = null;
		String commentary = null;

		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}

			String name = parser.getName();
			System.out.println("@@@@ COMBO -> " + name);

			if (name.equals("heroName")) {
				heroName = parser.getText();
			} else if (name.equals("commentary")) {
				commentary = parser.getText();
			}

		}
		return new Combo(heroName, commentary);
	}

	private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
		if (parser.getEventType() != XmlPullParser.START_TAG) {
			throw new IllegalStateException();
		}
		int depth = 1;
		while (depth != 0) {
			switch (parser.next()) {
			case XmlPullParser.END_TAG:
				depth--;
				break;
			case XmlPullParser.START_TAG:
				depth++;
				break;
			}
		}
	}
}
